<?php
/**
 * Created by PhpStorm.
 * User: gesangdianzi
 * Date: 2018/5/22
 * Time: 22:19
 */

namespace app\controllers;


use yii\log\Logger;
use yii\web\Controller;

class WxController extends Controller
{
    public function actionIndex(){
        $xml = file_get_contents("php://input");    // 获取post过来的xml数据
        if(!empty($xml)){
            $postObj = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
            \Yii::getLogger()->log(print_r($postObj,true),Logger::LEVEL_ERROR);
        }
//        $this->valid();
//        var_dump(\Yii::$app->request->post(),\Yii::$app->request->get());
    }

    private $token = "WEIXIN";
    private $appId = "wx47de60c4b5e2bf49";
    private $appSecret = "a1782675f93098621c9c2554f1b8409d";
    private function checkSignature() {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];
        $tmpArr = array($this->token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode($tmpArr);
        $tmpStr = sha1($tmpStr);
        if($tmpStr == $signature) {
            return true;
        } else {
            return false;
        }
    }

    public function valid()
    {
        $echoStr = $_GET["echostr"];               //valid signature, option
        if ($this->checkSignature()) {
            echo $echoStr;
            exit;
        }
    }
}